<template>
  <el-dialog :title="isShow===false? '新增房产':'编辑房产'" :visible="dialogFormVisible" @close="Close">
    <el-form ref="rulesForm" :model="form" :rules="rules">

      <el-form-item label="经纪" label-width="60px">
        <el-select v-model="form.agent" placeholder="请选择">
          <el-option
            v-for="item in jingji"
            :key="item.index"
            :label="item.type"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="分类" label-width="60px">
        <el-select v-model="form.category" placeholder="请选择">
          <el-option
            v-for="item in fenlei"
            :key="item.index"
            :label="item.type"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" label-width="60px" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>

      <el-form-item label="标签" label-width="60px">
        <el-tag
          v-for="tag in form.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">新增标签</el-button>
      </el-form-item>

      <el-form-item label-width="60px" label="城市">
        <el-cascader

          v-model="diqu"
          placeholder="输入市区名，选择省市区"
          :options="area"
          :separator="','"
          :props="props"
          @visible-change="check"
        />
      </el-form-item>

      <el-form-item label="地址" label-width="60px" prop="address">

        <el-input v-model="form.address" />

      </el-form-item>

      <el-form-item label="价格" label-width="60px" prop="price">
        <el-input v-model="form.price" />
      </el-form-item>

      <el-form-item label="描述" label-width="60px" prop="desc">
        <el-input
          v-model="form.desc"
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
        />
      </el-form-item>

      <el-form-item v-if="isShow" label="内容" label-width="60px">

        <div class="demo-image__preview">
          <el-image v-for="item in form.photos" :key="item.index" :src="item" style="width: 100px; height: 100px" />
        </div>
      </el-form-item>

      <el-form-item label="介绍图片" label-width="80px">
        <!-- 介绍上传图片 -->
        <upLoadShow ref="show" />
      </el-form-item>

      <el-form-item label="选择封面" label-width="80px">
        <upLoadCover ref="cover" />
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="Close">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import esimate from '@/api/estimate' // 选中类型
import { area } from '@/views/property/area' // 地区文件
import upLoadShow from '@/views/property/componments/upLoadShow' // 介绍图片上传组件
import upLoadCover from '@/views/property/componments/upLoadCover'// 封面组件
import { addOne, editOne } from '@/api/property' // 添加 跟编辑 请求

export default {
  components: {
    upLoadShow, // 介绍图片组件
    upLoadCover // 封面组件
  },
  props: {
    dialogFormVisible: Boolean, // 组件弹窗开关
    isShow: Boolean, // 弹窗标题 判断  true 新增 false编辑
    eidtId: {
      type: Number,
      default: 0
    }, // 房产id
    eidtInfo: {
      type: Object,
      default() {
        return {}// 回填数据
      }
    }},

  data() {
    return {
      form: {
        title: '假标题', // 弹窗标题
        desc: '', // 描述
        image: '', // 封面链接
        agent: '请选择经纪', // 哪个经纪
        content: '直接默认', // 可有可无
        address: '', // 地址
        price: '', // 价格
        photos: '', // 介绍图片
        tags: [ // 标签页

        ],
        category: '请选择分类' // 选择分类
      },
      rules: { // 校验规则
        title: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      jingji: esimate.broker, // 经纪人
      fenlei: esimate.classify, // 分类
      dynamicTags: [], // 写好的标签
      inputVisible: false, // 是否显示输入
      inputValue: '', // 地址输入

      area,
      diqu: '',
      props: {
        value: 'value',
        label: 'value'
      },
      shuru: 0

    }
  },

  methods: {
    async  btnOK() { // 用户点击确认后操作
      await this.$refs.rulesForm.validate()

      if (this.isShow === true) { // 编辑逻辑
        // console.log('编辑请求')
        await editOne(this.eidtId, this.form)
      } else {
        // 新增逻辑
        this.form.image = this.$refs.cover.fileList[0] ? this.$refs.cover.fileList[0].url : ''
        this.form.photos = this.$refs.show.fileList // 取消按发送给父组件弹出来操作关闭
        console.log(this.form, '看看拿到什么')
        await addOne(this.form)
      }

      this.$nextTick(item => {
        this.$emit('render') // 重新渲染
        this.$refs.rulesForm.resetFields() // 清理报错
        this.$emit('close') // 关闭弹窗
        this.$message.success('操作成功') // 提示用户
      })
    },
    Close() {
      this.$emit('close') // 关闭弹窗
      this.diqu = [] // 清除级联选择器选项
      // 恢复表单默认
      this.form = { // 表单数据
        title: '', // 弹窗标题
        desc: '', // 描述
        image: '', // 封面链接
        agent: '请选择经纪', // 哪个经纪
        content: '直接默认', // 可有可无
        address: '', // 地址
        price: '', // 价格
        photos: '', // 介绍图片
        tags: [ // 标签页

        ],
        category: '请选择分类' // 选择分类
      }

      // console.log(this.$refs.shuru.$options, '输入')
    },
    handleInputConfirm() { // 级联搜索下拉选项组件方法
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
        this.form.tags = this.dynamicTags
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    showInput() { // 添加标签输入框 失去焦点隐藏
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleClose(tag) { // 标签组件绑定方法
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    check(a) {
      if (a === false) { // 如果地区选好隐藏后   a会变false
        setTimeout(item => { // 拼接选中好的数据，回填至input
          this.form.address = this.diqu.join(',')
        }, 100)
      }
    }

  }

}
</script>

<style lang="scss" scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

   .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  //  ::v-deep .el-input__inner{
  //   width: 400px;
  // }
</style>
